package cc.iits.demo.crane.api.vo;

import cc.iits.demo.crane.api.bo.Result;

import java.io.Serializable;
import java.util.HashMap;

/**
 * 通用请求参数
 * @author 易俊
 */
public class RestResponse extends HashMap<String,Object> implements Serializable {

    private static final long serialVersionUID = 4055259087400661458L;
    private static final String CODE_NAME="code";
    private static final int SUCCESS_CODE=200;
    private static final int ERROR_CODE=500;
    private static final String STATUS_NAME="status";
    private static final String MESSAGE_NAME="message";
    private static final String DATA_NAME="data";
    private static final String TOTAL_NAME="total";
    private static final String REQUEST_NAME="request";

    public static RestResponse success(){
        RestResponse response=new RestResponse();
        response.setCode(SUCCESS_CODE);
        response.setStatus(RestResponseStatus.success);
        return response;
    }

    public static <T> RestResponse success(Result<T> result){
        RestResponse response=new RestResponse();
        response.setCode(SUCCESS_CODE);
        response.setStatus(RestResponseStatus.success);
        response.setData(result.getData());
        response.setTotal(result.getTotal());
        return response;
    }

    public static <T> RestResponse success(T result){
        RestResponse response=new RestResponse();
        response.setCode(SUCCESS_CODE);
        response.setStatus(RestResponseStatus.success);
        response.setData(result);
        return response;
    }

//    public static <T> RestResponse success(RestRequest request){
//        RestResponse<T> response= RestResponse.success();
//        response.setRequest(request);
//        return response;
//    }

    public static RestResponse error(String message){
        RestResponse response=new RestResponse();
        response.setCode(ERROR_CODE);
        response.setStatus(RestResponseStatus.error);
        response.setMessage(message);
        return  response;
    }

//    public static <T> RestResponse error(String message, RestRequest request){
//        RestResponse<T> response=error(message);
//        response.setRequest(request);
//        return  response;
//    }

    public static RestResponse error(int code, String message){
        RestResponse response=error(message);
        response.setCode(code);
        return  response;
    }


//    public static <T> RestResponse error(int code, String message, RestRequest request){
//        RestResponse<T> response=error(code,message);
//        response.setRequest(request);
//        return  response;
//    }


    public RestResponse setCode(int code){
        this.put(CODE_NAME,code);
        return this;
    }

    public int getCode(){
        return (int)this.get(CODE_NAME);
    }

    public RestResponse setStatus(RestResponseStatus status){
        this.put(STATUS_NAME,status);
        return this;
    }

    public RestResponseStatus getStatus(){
        return (RestResponseStatus)this.get(STATUS_NAME);
    }

    public RestResponse setMessage(String message){
        this.put(MESSAGE_NAME,message);
        return this;
    }

    public String getMessage(){
        return (String)this.get(MESSAGE_NAME);
    }

    public RestResponse setData(Object value){
        this.put(DATA_NAME,value);
        return this;
    }

    public <T> T getData(){
        return (T)this.get(DATA_NAME);
    }

    public RestResponse setTotal(long total){
        this.put(TOTAL_NAME,total);
        return this;
    }

    public long getTotal(){
        return (int)this.get(TOTAL_NAME);
    }


//    public void setRequest(RestRequest request){
//        this.put(REQUEST_NAME,request.toString());
//    }

//    public RestRequest getRequest(){
//        String json=(String)this.get(REQUEST_NAME);
//        return StringUtils.isNotBlank(json)? RestRequest.parseJson(json):null;
//    }
}
